package cn.ecut.servlet;

import cn.ecut.model.Comment;
import cn.ecut.model.User;
import cn.ecut.services.Commentservices;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CommentPublishServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String comment=req.getParameter("comment");
        String newsid=req.getParameter("newsid");
        Object o=req.getSession().getAttribute("user");
        String msg="";
        User user=null;
        if(o instanceof User ){
            user=(User) o;
        }else{
            msg="没有登陆。";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("false.jsp").forward(req,resp);
        }

        Comment comment1=new Comment();
        comment1.setNewsid(newsid);
        comment1.setContext(comment);
        comment1.setUserid(user.getId());
        comment1.setUsername(user.getUsername());
        Commentservices commentservices=new Commentservices();
        if(commentservices.AddComment(comment1)){
            msg="发布成功。";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("sucess.jsp").forward(req,resp);
        }else{
            msg="发布失败。";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("false.jsp").forward(req,resp);
        }

    }
}
